package com.sun.baseframework;

import com.baomidou.mybatisplus.annotation.DbType;
import com.baomidou.mybatisplus.generator.AutoGenerator;
import com.baomidou.mybatisplus.generator.config.DataSourceConfig;
import com.baomidou.mybatisplus.generator.config.GlobalConfig;
import com.baomidou.mybatisplus.generator.config.PackageConfig;
import com.baomidou.mybatisplus.generator.config.StrategyConfig;
import com.baomidou.mybatisplus.generator.config.rules.NamingStrategy;


/**
 * <p>
 * 代码生成器演示
 * </p>
 */
public class MpGenerator {
    private static final String packageName="com.sun.baseframework";    //文件路径

    private static final String authorName="sun";     //作者

    private static final String dbUrl = "jdbc:mysql://127.0.0.1:3306/test?characterEncoding=utf8";
    private static final String dbUser="root";
    private static final String dbPassword="111111";

    private static final String table="user"; //table名字

    public static void main(String[] args) {
        boolean serviceNameStartWith = false;
        new MpGenerator().generateByTables(serviceNameStartWith, packageName, table);
    }

    private void generateByTables(boolean serverNameStartWith, String packageName, String... tableName){
        GlobalConfig config = new GlobalConfig();

        DataSourceConfig dataSourceConfig = new DataSourceConfig();
        dataSourceConfig.setDbType(DbType.MYSQL)
                .setUrl(dbUrl)
                .setDriverName("com.mysql.jdbc.Driver")
                .setUsername(dbUser)
                .setPassword(dbPassword);

        StrategyConfig strategyConfig = new StrategyConfig();
        strategyConfig
                .setDbColumnUnderline(true)//全局下划线命名
                .setNaming(NamingStrategy.underline_to_camel)// 表名生成策略
                .setInclude(new String[] { table }) // 需要生成的表
                .setRestControllerStyle(true)
                .setInclude(tableName);
        config.setActiveRecord(false)
                .setAuthor(authorName)
                .setOutputDir("src\\main\\java")
                .setFileOverride(true);

        if(!serverNameStartWith){
            config.setServiceImplName("%sService");
        }

        new AutoGenerator().setGlobalConfig(config)
                .setDataSource(dataSourceConfig)
                .setStrategy(strategyConfig)
                .setPackageInfo(
                        new PackageConfig()
                        .setParent(packageName)
                        .setController("controller")
                        .setEntity("entity")
                ).execute();

    }
}

